package org.xtext.ic.dsl.generator;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.generator.IGenerator;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.IteratorExtensions;
import org.xtext.ic.dsl.generator.HibernateAnnotationsDAOExtensions;
import org.xtext.ic.dsl.generator.HibernateAnnotationsSessionExtensions;
import org.xtext.ic.dsl.generator.ModelExtensions;
import org.xtext.ic.dsl.icDsl.Connection;
import org.xtext.ic.dsl.icDsl.CreateDAO;
import org.xtext.ic.dsl.icDsl.Entity;
import org.xtext.ic.dsl.icDsl.Layer;
import org.xtext.ic.dsl.icDsl.Model;
import org.xtext.ic.dsl.icDsl.PackagePrefix;

@SuppressWarnings("all")
public class IcDslGenerator implements IGenerator {
  @Inject
  private IQualifiedNameProvider nameProvider;
  
  @Inject
  private ModelExtensions models;
  
  @Inject
  private HibernateAnnotationsSessionExtensions hibernateSession;
  
  @Inject
  private HibernateAnnotationsDAOExtensions hibernateDAO;
  
  public void doGenerate(final Resource resource, final IFileSystemAccess fsa) {
    EList<EObject> _contents = resource.getContents();
    EObject _head = IterableExtensions.<EObject>head(_contents);
    Model m = ((Model) _head);
    TreeIterator<EObject> _allContents = resource.getAllContents();
    Iterable<EObject> _iterable = IteratorExtensions.<EObject>toIterable(_allContents);
    Iterable<CreateDAO> _filter = Iterables.<CreateDAO>filter(_iterable, CreateDAO.class);
    CreateDAO cdao = IterableExtensions.<CreateDAO>head(_filter);
    TreeIterator<EObject> _allContents_1 = resource.getAllContents();
    Iterable<EObject> _iterable_1 = IteratorExtensions.<EObject>toIterable(_allContents_1);
    Iterable<Connection> _filter_1 = Iterables.<Connection>filter(_iterable_1, Connection.class);
    Connection conn = IterableExtensions.<Connection>head(_filter_1);
    PackagePrefix pp = m.getPackage();
    TreeIterator<EObject> _allContents_2 = resource.getAllContents();
    Iterable<EObject> _iterable_2 = IteratorExtensions.<EObject>toIterable(_allContents_2);
    Iterable<Entity> _filter_2 = Iterables.<Entity>filter(_iterable_2, Entity.class);
    for (final Entity e : _filter_2) {
      {
        QualifiedName _fullyQualifiedName = this.nameProvider.getFullyQualifiedName(e);
        List<String> layerFileNames = _fullyQualifiedName.getSegments();
        String entityFileName = "";
        int i = 1;
        boolean _notEquals = (!Objects.equal(pp, null));
        if (_notEquals) {
          String _name = pp.getName();
          String[] prefixFileNames = _name.split("\\.");
          int j = 1;
          for (final String fileName : prefixFileNames) {
            {
              String _plus = (entityFileName + fileName);
              String _plus_1 = (_plus + "/");
              entityFileName = _plus_1;
              int _plus_2 = (j + 1);
              j = _plus_2;
            }
          }
        }
        for (final String fileName_1 : layerFileNames) {
          {
            int _size = layerFileNames.size();
            boolean _lessThan = (i < _size);
            if (_lessThan) {
              String _plus = (entityFileName + fileName_1);
              String _plus_1 = (_plus + "/");
              entityFileName = _plus_1;
            } else {
              String _plus_2 = (entityFileName + fileName_1);
              String _plus_3 = (_plus_2 + ".java");
              entityFileName = _plus_3;
            }
            int _plus_4 = (i + 1);
            i = _plus_4;
          }
        }
        CharSequence _compile = this.models.compile(m, e);
        fsa.generateFile(entityFileName, _compile);
      }
    }
    boolean _notEquals = (!Objects.equal(conn, null));
    if (_notEquals) {
      EObject _eContainer = conn.eContainer();
      QualifiedName _fullyQualifiedName = this.nameProvider.getFullyQualifiedName(_eContainer);
      List<String> layerFileNames = _fullyQualifiedName.getSegments();
      String connFileName = "";
      String factConnFileName = "";
      boolean _notEquals_1 = (!Objects.equal(pp, null));
      if (_notEquals_1) {
        String _name = pp.getName();
        String[] prefixFileNames = _name.split("\\.");
        int i = 1;
        for (final String fileName : prefixFileNames) {
          {
            String _plus = (connFileName + fileName);
            String _plus_1 = (_plus + "/");
            connFileName = _plus_1;
            int _plus_2 = (i + 1);
            i = _plus_2;
          }
        }
      }
      for (final String fileName_1 : layerFileNames) {
        String _plus = (connFileName + fileName_1);
        String _plus_1 = (_plus + "/");
        connFileName = _plus_1;
      }
      String _plus_2 = (connFileName + "FabricaSessao.java");
      factConnFileName = _plus_2;
      String _plus_3 = (connFileName + "Sessao.java");
      connFileName = _plus_3;
      CharSequence _interfaceSessao = this.hibernateSession.interfaceSessao(m, conn);
      fsa.generateFile(connFileName, _interfaceSessao);
      boolean _notEquals_2 = (!Objects.equal(cdao, null));
      if (_notEquals_2) {
        CharSequence _fabricaSessao = this.hibernateSession.fabricaSessao(m, conn, cdao);
        fsa.generateFile(factConnFileName, _fabricaSessao);
        String daoFileName = "";
        String daoInterfaceFileName = "";
        String daoImplFileName = "";
        String baseDaoFileName = "";
        Layer _daolayer = cdao.getDaolayer();
        boolean _notEquals_3 = (!Objects.equal(_daolayer, null));
        if (_notEquals_3) {
          Layer _daolayer_1 = cdao.getDaolayer();
          QualifiedName _fullyQualifiedName_1 = this.nameProvider.getFullyQualifiedName(_daolayer_1);
          List<String> _segments = _fullyQualifiedName_1.getSegments();
          layerFileNames = _segments;
        } else {
          ArrayList<String> _arrayList = new ArrayList<String>();
          layerFileNames = _arrayList;
        }
        boolean _notEquals_4 = (!Objects.equal(pp, null));
        if (_notEquals_4) {
          String _name_1 = pp.getName();
          String[] prefixFileNames_1 = _name_1.split("\\.");
          int i_1 = 1;
          for (final String fileName_2 : prefixFileNames_1) {
            {
              String _plus_4 = (daoFileName + fileName_2);
              String _plus_5 = (_plus_4 + "/");
              daoFileName = _plus_5;
              int _plus_6 = (i_1 + 1);
              i_1 = _plus_6;
            }
          }
        }
        for (final String fileName_3 : layerFileNames) {
          String _plus_4 = (daoFileName + fileName_3);
          String _plus_5 = (_plus_4 + "/");
          daoFileName = _plus_5;
        }
        Entity _firstEntity = cdao.getFirstEntity();
        boolean _notEquals_5 = (!Objects.equal(_firstEntity, null));
        if (_notEquals_5) {
          String _plus_6 = (daoFileName + "BaseDAO.java");
          baseDaoFileName = _plus_6;
          Entity _firstEntity_1 = cdao.getFirstEntity();
          String _name_2 = _firstEntity_1.getName();
          String _plus_7 = (daoFileName + _name_2);
          String _plus_8 = (_plus_7 + "DAO.java");
          daoInterfaceFileName = _plus_8;
          Entity _firstEntity_2 = cdao.getFirstEntity();
          String _name_3 = _firstEntity_2.getName();
          String _plus_9 = (daoFileName + _name_3);
          String _plus_10 = (_plus_9 + "DAOImpl.java");
          daoImplFileName = _plus_10;
          CharSequence _baseDAO = this.hibernateDAO.baseDAO(m, conn, cdao);
          fsa.generateFile(baseDaoFileName, _baseDAO);
          Entity _firstEntity_3 = cdao.getFirstEntity();
          CharSequence _entityDAO = this.hibernateDAO.entityDAO(m, cdao, _firstEntity_3);
          fsa.generateFile(daoInterfaceFileName, _entityDAO);
          Entity _firstEntity_4 = cdao.getFirstEntity();
          CharSequence _entityDAOImpl = this.hibernateDAO.entityDAOImpl(m, conn, cdao, _firstEntity_4);
          fsa.generateFile(daoImplFileName, _entityDAOImpl);
          EList<Entity> _entities = cdao.getEntities();
          for (final Entity e_1 : _entities) {
            {
              String _name_4 = e_1.getName();
              String _plus_11 = (daoFileName + _name_4);
              String _plus_12 = (_plus_11 + "DAO.java");
              daoInterfaceFileName = _plus_12;
              String _name_5 = e_1.getName();
              String _plus_13 = (daoFileName + _name_5);
              String _plus_14 = (_plus_13 + "DAOImpl.java");
              daoImplFileName = _plus_14;
              CharSequence _entityDAO_1 = this.hibernateDAO.entityDAO(m, cdao, e_1);
              fsa.generateFile(daoInterfaceFileName, _entityDAO_1);
              CharSequence _entityDAOImpl_1 = this.hibernateDAO.entityDAOImpl(m, conn, cdao, e_1);
              fsa.generateFile(daoImplFileName, _entityDAOImpl_1);
            }
          }
        }
      }
    }
  }
}
